package fr.bdf.center.birt.web.comment.print;

import org.eclipse.birt.core.exception.BirtException;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.engine.api.EngineConfig;
import org.eclipse.birt.report.engine.api.EngineConstants;
import org.eclipse.birt.report.engine.api.EngineException;
import org.eclipse.birt.report.engine.api.IReportEngine;
import org.eclipse.birt.report.engine.api.IReportEngineFactory;
import org.eclipse.birt.report.engine.api.IReportRunnable;
import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
import org.eclipse.birt.report.engine.api.PDFRenderOption;
import org.eclipse.birt.report.engine.api.RenderOption;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import fr.umlv.m2.jee.birt.service.comment.print.IPrintableCommentService;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.Locale;

public class PrintCommentServlet extends HttpServlet {

    private static final long serialVersionUID = -2472892306983259670L;

    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        final EngineConfig config = new EngineConfig();

        final ServletContext context = getServletContext();
        WebApplicationContext applicationContext =  WebApplicationContextUtils.getWebApplicationContext(context);

        IPrintableCommentService printableCommentService = (IPrintableCommentService)applicationContext.getBean("printableCommentService");

        try {
            final ServletContext sc = req.getSession().getServletContext();
            // Definition du repertoire contenant les binaires BIRT, ce
            // repertoire est ajouter par Maven lors de la generation du paquet

            config.setEngineHome(sc.getRealPath("WEB-INF/ReportEngine"));

            Platform.startup(config); // Application de la configuration BIRT

            final IReportEngineFactory factory = (IReportEngineFactory)Platform.createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
            final IReportEngine engine = factory.createReportEngine(config);

            // Definition des parametres de generation
            final RenderOption option = new PDFRenderOption();
            option.setOutputFormat(PDFRenderOption.OUTPUT_FORMAT_PDF);

            // Selection du rapport BIRT permettant la generation
            final IReportRunnable design = engine.openReportDesign(sc.getResourceAsStream("WEB-INF/Reports/comment.rptdesign"));

            final IRunAndRenderTask task = engine.createRunAndRenderTask(design);

            task.getAppContext().put(EngineConstants.APPCONTEXT_CLASSLOADER_KEY, Thread.currentThread().getContextClassLoader());

            task.setParameterValue("comment", printableCommentService.getPrintableComment());

            // Association de la generation a la sortie de la Servlet
            option.setOutputStream(resp.getOutputStream());

            task.setLocale(Locale.FRENCH);
            task.setRenderOption(option);

            // Generation du document
            task.run();
        } catch(EngineException e) {
            throw new ServletException(e);
        } catch(BirtException e) {
            throw new ServletException(e);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}
